#!/usr/bin/bash
. /etc/init.d/functions
version=5.0.14
installDir=/apps/redis
srcDir=/usr/local/src
user=redis

function installTools() {
    yum install -y $@ || { action "安装软件包失败，请检查网络配置" false; exit; }
}

function downloadAndUnzip() {
    cd ${srcDir}
    wget http://download.redis.io/releases/redis-${version}.tar.gz || { action "Redis 源码下载失败" false ; exit; }
    tar xf redis-${version}.tar.gz || { action "Redis 源码解压失败" false ; exit; }
}

function installRedis() {
    cd redis-${version}
    make PREFIX=${installDir} install && action "Redis 编译安装完成" || { action "Redis 编译安装失败" false ;exit ; }
}

function configSetup() {
    ln -s ${installDir}/bin/redis-* /usr/bin/
    mkdir -p ${installDir}/{etc,log,data,run}
    cp redis.conf ${installDir}/etc/ && action "成功复制redis.conf文件" || { action "redis.conf文件复制失败" false; exit; }
    #sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/# requirepass/a requirepass $PASSWORD" -e "/^dir .*/c dir ${INSTALL_DIR}/data/" -e "/logfile .*/c logfile ${INSTALL_DIR}/log/redis-6379.log" -e "/^pidfile .*/c pidfile ${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf
    sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e "/^dir .*/c dir ${installDir}/data/" -e "/logfile .*/c logfile ${installDir}/log/redis-6379.log" -e "/^pidfile .*/c pidfile ${installDir}/run/redis_6379.pid" ${installDir}/etc/redis.conf

    if id ${user} &> /dev/null ;then
      action "${user} 用户已存在" false
    else
      useradd -r -s /sbin/nologin redis
      action "${user} 用户创建成功"
    fi

    chown -R redis.redis ${installDir}

    overcommitHead=$(grep vm.overcommit_memory /etc/sysctl.conf | grep -o '#')
    if [[ ${overcommitHead} == '#' ]];then
        echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
    else
      overcommitValue=$(grep vm.overcommit_memory /etc/sysctl.conf | awk '{print $NF}')

      if [[ ${overcommitValue} == "" ]];then
        echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
      elif [[ ${overcommitValue} == "0" ]];then
        sed -i 's#vm.overcommit_memory = 0#vm.overcommit_memory = 1#' /etc/sysctl.conf
      fi
    fi

    somaxHead=$(grep net.core.somaxconn /etc/sysctl.conf | grep -o '#')
    if [[ ${somaxHead} == '#' ]];then
        echo 'net.core.somaxconn = 32768' >> /etc/sysctl.conf
    else
      somaxValue=$(grep net.core.somaxconn /etc/sysctl.conf | awk '{print $NF}')

      if [[ ${somaxValue} == "" ]];then
        echo 'net.core.somaxconn = 32768' >> /etc/sysctl.conf
      elif [[ ${somaxValue} < "1024" ]];then
        sed -i 's#net.core.somaxconn = '"${somaxValue}"'#net.core.somaxconn = 32768#' /etc/sysctl.conf
      fi
    fi

    sysctl -p

    echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
    chmod +x /etc/rc.d/rc.local
    /etc/rc.d/rc.local

cat > /usr/lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
#Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF
}

function startRedis() {
    systemctl daemon-reload
    chown -R redis.redis ${installDir}
    systemctl enable --now redis.service &> /dev/null && action "Redis 服务启动成功,Redis信息如下:" || { action "Redis 启动失败" false ;exit; }
}

function installNewGcc() {
echo '
# CentOS-SCLo-sclo.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/SCLo for more
# information

[centos-sclo-sclo]
name=CentOS-7 - SCLo sclo
baseurl=http://mirrors.aliyun.com/centos/7/sclo/$basearch/sclo/
# mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-sclo
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-sclo-testing]
name=CentOS-7 - SCLo sclo Testing
baseurl=http://mirrors.aliyun.com/centos/7/sclo/$basearch/sclo/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-sclo-source]
name=CentOS-7 - SCLo sclo Sources
baseurl=http://mirrors.aliyun.com/centos/7/sclo/Source/sclo/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-sclo-debuginfo]
name=CentOS-7 - SCLo sclo Debuginfo
baseurl=http://mirrors.aliyun.com/centos/7/sclo/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo' > /etc/yum.repos.d/CentOS-SCLo-scl.repo

echo '# CentOS-SCLo-rh.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/SCLo for more
# information

[centos-sclo-rh]
name=CentOS-7 - SCLo rh
baseurl=http://mirrors.aliyun.com/centos/7/sclo/$basearch/rh/
#mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-rh
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-rh-testing]
name=CentOS-7 - SCLo rh Testing
baseurl=http://mirrors.aliyun.com/centos/7/sclo/$basearch/rh/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-rh-source]
name=CentOS-7 - SCLo rh Sources
baseurl=http://mirrors.aliyun.com/centos/7/sclo/Source/rh/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo

[centos-sclo-rh-debuginfo]
name=CentOS-7 - SCLo rh Debuginfo
baseurl=http://mirrors.aliyun.com/centos/7/sclo/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-SCLo' >  /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
    yum install -y devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutils && action "gcc 11 安装完成" || { action "gcc 11 安装失败" false ;exit; }
    source /opt/rh/devtoolset-11/enable
}

function versionCheckGcc() {
    if [[ $(which gcc) ]];then
      gccVersion=$(gcc --version | head -n1 | cut -d " " -f3 | cut -d "." -f1)
      if [[ ${gccVersion} == "4" ]] || [[ ${gccVersion} == "5" ]];then
        installNewGcc
      fi
    else
      installNewGcc
    fi
}
function main() {
    installVersion=$(echo ${version} | awk -F "." '{print $1}')
    case ${installVersion} in
    4|5)
      installTools gcc jemalloc-devel
      downloadAndUnzip
      installRedis
      configSetup
      startRedis
      ;;
    6|7)
      installTools jemalloc-devel centos-release-scl
      versionCheckGcc
      downloadAndUnzip
      installRedis
      configSetup
      startRedis
      ;;
    esac
}

main